package com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.HomePageFragmentActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/16.
 * 第一个界面
 */
public class MainActivity extends Activity implements View.OnClickListener {

    public final int MSG_FINISH_LAUNCHERACTIVITY = 500;
   private  int  SKIP =5000;
    public Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_FINISH_LAUNCHERACTIVITY:
                    //跳转到MainActivity，并结束当前的LauncherActivity
                    Intent intent = new Intent(MainActivity.this, HomePageFragmentActivity.class);
                    startActivity(intent);
                    finish();
                    break;

                default:
                    break;
            }
        }

        ;
    };
    @Bind(R.id.jump)
    TextView jump;
    private MyCountDownTimer mc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 不显示系统的标题栏，保证windowBackground和界面activity_main的大小一样，显示在屏幕不会有错位（去掉这一行试试就知道效果了）
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mc = new MyCountDownTimer(SKIP, 1000);
        mc.start();
        // 停留3秒后发送消息，跳转到MainActivity
        mHandler.sendEmptyMessageDelayed(MSG_FINISH_LAUNCHERACTIVITY,SKIP);
        jump.setOnClickListener(this);
    }

    /**
     * 继承 CountDownTimer 防范
     * <p>
     * 重写 父类的方法 onTick() 、 onFinish()
     */
    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        public void onFinish() {
            jump.setText("正在跳转");
        }

        public void onTick(long millisUntilFinished) {
            jump.setText("倒计时(" + millisUntilFinished / 1000 + "s)");
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, HomePageFragmentActivity.class);
        startActivity(intent);
        mHandler.removeMessages(MSG_FINISH_LAUNCHERACTIVITY);
        finish();
    }
}
